import pandas as pd
from sqlalchemy import create_engine


def chan_calc(high, low):
    _high = max(high)
    _low = min(low)
    _mean = (_high + _low) / 2
    flag = [_high, _low, _mean]
    return flag


def average_true_range(ltr):
    atr = sum(ltr) / len(ltr)
    return float(atr)


def true_range(close, high, low):
    tr = '%.2f' % float(max((high - low), (high - close), (close - low)))
    return float(tr)


class SHControl():
    """
    true_range:
                传入昨日收盘价close，当日最高价high，当日最低价low
                返回真实波幅tr：浮点数
    average_true_range:
                以列表传入N日的真实波幅ltr
                返回N日波幅平均值atr：浮点数
    channel_calc:
                传入一个最高价的列表，和一个最低价的列表
                传出一个列表，[_high,_low,_mean]分别是最高值，最低值，平均值
    """

# class Channel():
#
#     """
#     channel_calc:
#                 传入一个最高价的列表，和一个最低价的列表
#                 传出一个列表，[_high,_low,_mean]分别是最高值，最低值，平均值
#     """
#


# class Main():
#
#     def __init__(self):
#         self.engine = create_engine(r'mysql+pymysql://root:mySQL.root.4415@localhost:3306/Stock?charset=utf8')
#
#     def read_data(self):
#         tab = 'select * from {} order by trade_date DESC limit 20;'
#         table = '688699_SH'
#         df = pd.read_sql(tab.format(table), self.engine)
#         highs = []
#         lows = []
#         for row in df.itertuples():
#             high = getattr(row, 'high')
#             highs.append(high)
#             low = getattr(row, 'low')
#             lows.append(low)
#         cc = Channel().channel_calc(highs, lows)
#         return cc

# if __name__ == '__main__':
#     Main().read_data()
